'''
Benchmark for utils.release_helper
'''
from utils.release_helper import IndexReleaseHelper

from . import ProducerCounter
from . import SQL_DATA


def release_index(task, conn, storage):
    '''Release indices'''
    sql_template = 'SELECT name, timestamp FROM %s'
    producer = ProducerCounter()
    helper = IndexReleaseHelper(task=task, producer=producer, conn=conn,
                                storage=storage)
    helper.release(drift=0, date='date', sql_template=sql_template)
    return producer.index


def test_index_benchmark(task, conn, storage, benchmark):
    '''Benchmark for IndexReleaseHelper.release'''
    with conn.cursor() as cur:
        for timestamp in range(1000):
            for _ in range(100):
                cur.execute(SQL_DATA, ('name', timestamp))
    conn.commit()

    task.topic = 'topic'
    task.tables = ['test', ]

    count = benchmark(release_index, task=task, conn=conn, storage=storage)
    assert count == 1000
